package top.korori.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.korori.common.exception.BizException;
import top.korori.entity.SysRole;
import top.korori.entity.SysRoleMenu;
import top.korori.entity.bo.SysRoleBo;
import top.korori.entity.vo.SysRoleVo;
import top.korori.enums.SysRoleStatus;
import top.korori.mapper.SysRoleMapper;
import top.korori.service.SysMenuService;
import top.korori.service.SysRoleMenuService;
import top.korori.service.SysRoleService;

import java.util.List;

/**
 * 
 * 后台角色表 服务实现类
 * 
 *
 * @author korori
 * @since
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public SysRoleVo getRoleMenu(Long roleId) {
        SysRole sysRole = this.getById(roleId);
        SysRoleVo sysRoleVo = BeanUtil.copyProperties(sysRole, SysRoleVo.class);
        List<Long> menuIds = sysMenuService.treeMenuByRoleId(roleId);
        sysRoleVo.setMenuIds(menuIds);
        return sysRoleVo;
    }

    @Override
    public void add(SysRoleBo sysRoleBo) {
        checkRole(sysRoleBo);
        SysRole sysRole = BeanUtil.copyProperties(sysRoleBo, SysRole.class);
        this.save(sysRole);
        handleMenu(sysRoleBo, sysRole);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SysRoleBo sysRoleBo) {
        checkRole(sysRoleBo);
        if (ObjUtil.isEmpty(sysRoleBo.getRoleId())){
            BizException.cast("角色不能id为空");
        }
        SysRole sysRole = BeanUtil.copyProperties(sysRoleBo, SysRole.class);
        this.updateById(sysRole);
        sysRoleMenuService.delByRoleId(sysRoleBo.getRoleId());
        handleMenu(sysRoleBo, sysRole);
    }

    private void handleMenu(SysRoleBo sysRoleBo, SysRole sysRole) {
        if (ObjectUtil.isEmpty(sysRoleBo.getMenuIds())) {
            return;
        }

        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleBo.getMenuIds().forEach(menuId -> {
            sysRoleMenu.setId(null);
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuService.save(sysRoleMenu);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> roleIds) {
        roleIds.forEach(roleId -> {
            this.removeById(roleId);
            sysRoleMenuService.delByRoleId(roleId);
        });
    }

    @Override
    public List<SysRole> getNormal() {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getStatus, SysRoleStatus.NORMAL.getCode());
        queryWrapper.orderByAsc(SysRole::getSort);
        return this.list(queryWrapper);
    }

    private void checkRole (SysRoleBo sysRoleBo){
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(ObjUtil.isNotEmpty(sysRoleBo.getRoleId()), SysRole::getRoleId,sysRoleBo.getRoleId());
        queryWrapper.and(qr -> {
            qr.eq(SysRole::getRoleName,sysRoleBo.getRoleName());
            qr.or().eq(SysRole::getRoleKey,sysRoleBo.getRoleKey());
        });
        SysRole sysRole = this.getOne(queryWrapper, false);
        if (ObjUtil.isNotNull(sysRole)){
            if (ObjUtil.equal(sysRole.getRoleName(),sysRoleBo.getRoleName())){
                BizException.cast("角色名已存在");
            }
            if (ObjUtil.equal(sysRole.getRoleKey(),sysRoleBo.getRoleKey())){
                BizException.cast("角色字符已存在");
            }
        }
    }
}


















